package net.jbot.loader;

import java.applet.AppletContext;
import java.applet.AppletStub;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

public class BotStub implements AppletStub {

	public static final HashMap<String, String> parameters = new HashMap<String, String>();
	String referer = "http://oldschool.runescape.com/slu";
	public String gp;

	public BotStub() throws Exception {
		System.out.println("Reading page.");
		String source = readPage(getCodeBase(), referer);
		System.out.println("Parsing parameters.");
		parseParameters(source);
		System.out.println("Getting gamepack.");
		gp = getGamePack(source);
	}

	/**
	 * This gets the gamepack.jar from the web page source.
	 */
	private String getGamePack(String source) {
		String gpfBegin = "archive=";
		int gpfBeginIndex = source.indexOf(gpfBegin);
		return source.substring(gpfBeginIndex + gpfBegin.length(),
				source.indexOf('\'', gpfBeginIndex));
	}

	/**
	 * What this does is get the source from the selected web page.
	 */
	private String readPage(URL url, String referer) throws IOException,
			InterruptedException {
		URLConnection uc = url.openConnection();
		uc.addRequestProperty(
				"Accept",
				"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
		uc.addRequestProperty("Accept-Charset",
				"ISO-8859-1,utf-8;q=0.7,*;q=0.7");
		uc.addRequestProperty("Accept-Encoding", "gzip,deflate");
		uc.addRequestProperty("Accept-Language", "en-gb,en;q=0.5");
		uc.addRequestProperty("Connection", "keep-alive");
		uc.addRequestProperty("Host", "www.runescape.com");
		uc.addRequestProperty("Keep-Alive", "300");
		if (referer != null)
			uc.addRequestProperty("Referer", referer);
		uc.addRequestProperty(
				"User-Agent",
				"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6");
		DataInputStream di = new DataInputStream(uc.getInputStream());
		byte[] buffer = new byte[uc.getContentLength()];
		di.readFully(buffer);
		di.close();
		// Thread.sleep(250 + (int) ((((System.nanoTime() >> 256) / 1000000000)
		// / 10000)) * 500);
		return new String(buffer);
	}

	/**
	 * This, as the name suggests parses the parameters of the given web page
	 * source
	 */
	private void parseParameters(String source) {
		String[] lines = source.split("\n");
		String paramBegin = "param name=";
		String valueBegin = "value=";
		for (String line : lines) {
			if (!line.contains(paramBegin)) {
				continue;
			}
			int start = line.indexOf(paramBegin) + paramBegin.length() + 1;
			int end = line.indexOf('"', start);
			String name = line.substring(start, end);
			start = line.indexOf(valueBegin) + valueBegin.length() + 1;
			end = line.indexOf('"', start);
			String value = line.substring(start, end);
			parameters.put(name, value);
		}
	}

	public boolean isActive() {
		return true;
	}

	public URL getDocumentBase() {
		return getCodeBase();
	}

	public URL getCodeBase() {
		try {
			return new URL("http://oldschool20.runescape.com/");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String getParameter(String name) {
		return parameters.get(name);
	}

	public AppletContext getAppletContext() {
		return null;
	}

	public void appletResize(int width, int height) {
	}

}
